package com.zhuanzhuan.hero.user.service.impl;

import com.zhuanzhuan.hero.biz.common.enums.BizCountType;
import com.zhuanzhuan.hero.user.consts.NumConsts;
import com.zhuanzhuan.hero.user.enums.ExceptionMsgEnum;
import com.zhuanzhuan.hero.user.exception.BusinessException;
import com.zhuanzhuan.hero.user.helper.HeroCounterHelper;
import com.zhuanzhuan.hero.user.helper.HeroRecycleOrderHelper;
import com.zhuanzhuan.hero.user.mq.dto.BaseUserMsg;
import com.zhuanzhuan.hero.user.mq.producer.HeroUserProducer;
import com.zhuanzhuan.hero.user.service.ICounterService;
import com.zhuanzhuan.hero.user.util.AsyncUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @ClassName CounterServiceImpl
 * @Description
 * @Author hanjunjun
 * @Date 2024/7/3 17:05
 */
@Service
@Slf4j
public class CounterServiceImpl implements ICounterService {

    @Resource
    private HeroCounterHelper heroCounterHelper;

    @Resource
    private HeroRecycleOrderHelper heroRecycleOrderHelper;

    @Resource
    private HeroUserProducer heroUserProducer;

    @Override
    public boolean fraternityAuthUnreadIncre(Long uid) {
        log.info("同行认证未读数++ uid={}", uid);
        AsyncUtil.run(() -> {
            boolean reportCount =
                heroCounterHelper.reportCount(BizCountType.FELLOW_CERTIFICATE_UNREAD_COUNT, uid, NumConsts.ONE);
            if (!reportCount) {
                log.info("reportCount失败");
                throw new BusinessException("reportCount失败");
            }
        });

        return true;
    }

    @Override
    public Long queryDealSuccessRecycleOrderNum(Long fromUid, Long toUid) {
        if (Objects.isNull(fromUid) || Objects.isNull(toUid)) {
            throw new BusinessException(ExceptionMsgEnum.PARAM_ERROR);
        }
        Long num = heroRecycleOrderHelper.statisticsOrderNum(fromUid, toUid);
        return Objects.isNull(num) ? NumConsts.ZERO_L : num;
    }

    @Override
    public Long queryDealSuccessRecycleOrderNum(Long uid) {
        Long num = heroCounterHelper.queryTotal(BizCountType.USER_RECYCLE_ORDER_COUNT, uid);
        return Objects.isNull(num) ? NumConsts.ZERO_L : num;
    }

    @Override
    public Map<Long, Long> batchQueryDealSuccessRecycleOrderNum(List<Long> uids) {
        return heroCounterHelper.batchQueryTotal(BizCountType.USER_RECYCLE_ORDER_COUNT, uids);
    }

    @Override
    public boolean userReliableNumIncre(Long uid) {
        log.info("用户靠谱认证数++ uid={}", uid);
        boolean reportCount = heroCounterHelper.reportCount(BizCountType.RELIABLE_COUNT, uid, NumConsts.ONE);
        if (!reportCount) {
            throw new BusinessException("reportCount失败");
        }

        //发mq
        if (!heroUserProducer.sendReliableNumUpdate(new BaseUserMsg(uid))) {
            log.error("靠谱数变更发mq失败");
        }

        return true;
    }

    @Override
    public boolean userReliableNumDecre(Long uid) {
        log.info("用户靠谱认证数-- uid={}", uid);
        boolean reportCount = heroCounterHelper.reportCount(BizCountType.RELIABLE_COUNT, uid, -NumConsts.ONE);
        if (!reportCount) {
            throw new BusinessException("reportCount失败");
        }

        //发mq
        if (!heroUserProducer.sendReliableNumUpdate(new BaseUserMsg(uid))) {
            log.error("靠谱数变更发mq失败");
        }

        return true;
    }

    @Override
    public boolean userUnReliableNumIncre(Long uid) {
        log.info("用户不靠谱认证数++ uid={}", uid);
        boolean reportCount = heroCounterHelper.reportCount(BizCountType.UNRELIABLE_COUNT, uid, NumConsts.ONE);
        if (!reportCount) {
            throw new BusinessException("reportCount失败");
        }

        return true;
    }

    @Override
    public boolean userUnReliableNumDecre(Long uid) {
        log.info("用户不靠谱认证数-- uid={}", uid);
        boolean reportCount = heroCounterHelper.reportCount(BizCountType.UNRELIABLE_COUNT, uid, -NumConsts.ONE);
        if (!reportCount) {
            log.info("reportCount失败");
            throw new BusinessException("reportCount失败");
        }

        return true;
    }

    @Override
    public boolean reliableUnreadNumIncre(Long uid) {
        log.info("用户靠谱认证未读数++ uid={}", uid);
        AsyncUtil.run(() -> {
            boolean reportCount = heroCounterHelper.reportCount(BizCountType.RELIABLE_UNREAD_COUNT, uid, NumConsts.ONE);
            if (!reportCount) {
                log.info("reportCount失败");
                throw new BusinessException("reportCount失败");
            }
        });

        return true;
    }

    @Override
    public Long queryFollowedNum(Long uid) {
        return heroCounterHelper.queryTotal(BizCountType.USER_FOLLOWER_COUNT, uid);
    }

    @Override
    public Map<Long, Long> batchQueryFollowedNum(List<Long> uids) {
        return heroCounterHelper.batchQueryTotal(BizCountType.USER_FOLLOWER_COUNT, uids);
    }

    @Override
    public Long queryCertifiedReliableNum(Long uid) {
        return heroCounterHelper.queryTotal(BizCountType.RELIABLE_COUNT, uid);
    }

    @Override
    public Map<Long, Long> batchQueryCertifiedReliableNum(List<Long> uids) {
        return heroCounterHelper.batchQueryTotal(BizCountType.RELIABLE_COUNT, uids);
    }
}
